package com.threeti.mecool.core.domain.model.plan.aspects;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.threeti.mecool.core.domain.model.plan.pojo.Job;
import com.threeti.mecool.core.domain.model.plan.pojo.Step;
import com.threeti.mecool.core.infrastructure.persistence.plan.JobRepository;

privileged aspect Job_AJ_Domain {
	@Autowired
	@JsonIgnore
	private transient JobRepository Job.jobRepository;
	
	static JobRepository Job.jobRepository() {
		return new Job().jobRepository;
	}
	
	public static Job Job.findById(Long id) {
		return Job.jobRepository().findOne(id);
	}

	@Transactional
	public void Job.save() {
		if (jobRepository == null) {
			jobRepository = jobRepository();
		}
		for (int i = 0, n = steps.size(); i < n; i++) {
			Step step = steps.get(i);
			step.setStepsOrder(i);
		}
		jobRepository.save(this);
	}
}
